﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Configuration;

namespace Tools
{
    public static class ActionKey
    {
        public static bool CheckActionKey(string viewForder, string viewName, string Permissions, string ActionKey)
        {
            DateTime DT = (DateTime)HttpContext.Current.Session["AsynchronousLoginTime"];
            if (FormsAuthentication.HashPasswordForStoringInConfigFile(viewForder + viewName + HttpContext.Current.Session["AsynchronousUserID"] + DT.ToString("yyMMddHHmmss") + Permissions + ConfigurationManager.AppSettings["ActionKey"], "MD5") == ActionKey)
                return true;

            return false;
        }

        public static string CreateActionKey(string viewForder, string viewName, string Permissions)
        {
            DateTime DT = (DateTime)HttpContext.Current.Session["AsynchronousLoginTime"];
            return FormsAuthentication.HashPasswordForStoringInConfigFile(viewForder + viewName + HttpContext.Current.Session["AsynchronousUserID"] + DT.ToString("yyMMddHHmmss") + Permissions + ConfigurationManager.AppSettings["ActionKey"], "MD5");
        }

        public static void CheckActionKeyThrow(string viewForder, string viewName, string Permissions, string ActionKey)
        {
            if (!CheckActionKey(viewForder, viewName, Permissions, ActionKey))
                throw new Exception("无法通过系统验证，请遵从正常的流程进行操作或者重新登陆再试。");
        }
    }
}
